﻿

using System;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using log4net;
namespace Utility
{
    /// <summary>
    /// Used to send mail.
    /// </summary>
    public class MailMessage
    {
        #region Fields

        private string serverName;
        private string username;
        private string password;
        private string subject;
        private string body;
        private string from;
        private string to;
        private string cc;
        private string bcc;
        private bool ssl;
        private bool isHtml;
        private int port;

        private log4net.ILog logger;
        #endregion Fields

        #region Contructors
        public MailMessage()
        {
            this.serverName = string.Empty;
            this.port = 25;
            this.username = string.Empty;
            this.password = string.Empty;
            this.body = string.Empty;
            this.from = string.Empty;
            this.to = string.Empty;
            this.cc = string.Empty;
            this.bcc = string.Empty;
            this.subject = string.Empty;
            this.logger = LoggingUtility.GetLogger(typeof(MailMessage).FullName);
            this.ssl = false;
            this.isHtml = false;

        }
        public MailMessage(string serverName, int port, string from, string to, string subject, string body)
        {
            this.serverName = serverName;
            this.port = port;
            this.body = body;
            this.from = from;
            this.to = to;
            this.subject = subject;
            this.ssl = false;
            this.username = string.Empty;
            this.password = string.Empty;
            this.logger = LoggingUtility.GetLogger(typeof(MailMessage).FullName);
        }
        public MailMessage(string serverName, int port, bool ssl, string username, string password, string from, string to, string cc, string bcc, string subject, string body)
        {
            this.serverName = serverName;
            this.port = port;
            this.username = username;
            this.password = password;
            this.body = body;
            this.from = from;
            this.to = to;
            this.cc = cc;
            this.bcc = bcc;
            this.subject = subject;
            this.ssl = ssl;
            this.logger = LoggingUtility.GetLogger(typeof(MailMessage).FullName);
        }
        public MailMessage(string serverName, int port, bool ssl, string username, string password, string from, string to, string cc, string bcc, string subject, string body, bool isHtml)
        {
            this.serverName = serverName;
            this.port = port;
            this.username = username;
            this.password = password;
            this.body = body;
            this.from = from;
            this.to = to;
            this.cc = cc;
            this.bcc = bcc;
            this.subject = subject;
            this.ssl = ssl;
            this.isHtml = isHtml;
            this.logger = LoggingUtility.GetLogger(typeof(MailMessage).FullName);
        }
        #endregion Contructors

        #region Methods

        public bool SendMail()
        {
            try
            {
                SmtpClient client = new SmtpClient();
                client.Timeout = 60000;
                client.Host = this.serverName;
                client.Port = this.port;
                if (ssl)
                {
                    client.EnableSsl = true;
                }
                // Make an mail message to send over smtp client
                System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
                if (this.cc != string.Empty)
                    mailMessage.CC.Add(this.cc);
                if (this.bcc != string.Empty)
                    mailMessage.Bcc.Add(this.bcc);
                mailMessage.Subject = this.subject;
                mailMessage.Body = this.body;
                try
                {
                    mailMessage.From = new MailAddress(this.from);
                }
                catch
                {
                    return false;
                }
                try
                {
                    if (this.to.IndexOf(",") > 0)
                    {
                        string[] arr = this.to.Split(',');
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i].Length.ToString() != string.Empty)
                                mailMessage.To.Add(new MailAddress(arr[i].ToString()));                            
                        }
                    }
                }
                catch
                {
                    return false;
                }
                mailMessage.IsBodyHtml = this.isHtml;
                mailMessage.Priority = MailPriority.High;
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.Headers.Add("X-Company", "Company");
                mailMessage.Headers.Add("X-Location", "Vietnamese");
                try
                {
                    client.Send(mailMessage);
                    mailMessage.Dispose();
                    return true;
                }
                catch (Exception e)
                {
                    if (logger.IsErrorEnabled)
                        logger.Error(e.Message);
                    mailMessage.Dispose();
                    return false;
                }
            }
            catch
            { 
            }
            return false;
        }

        public bool SendMailHtml()
        {
            SmtpClient client = new SmtpClient();
            client.Timeout = 60000;
            client.Host = this.serverName;
            client.Port = this.port;
            if (this.username != string.Empty)
            {
                NetworkCredential cred = new NetworkCredential(this.username, this.password);
                client.Credentials = cred;
            }
            if (ssl)
            {
                client.EnableSsl = true;
            }
            // Make an mail message to send over smtp client
            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
            if (this.from != string.Empty)
                mailMessage.From = new MailAddress(this.from);
            if (this.to != string.Empty)
                mailMessage.To.Add(this.to);
            if (this.cc != string.Empty)
                mailMessage.CC.Add(this.cc);
            if (this.bcc != string.Empty)
                mailMessage.Bcc.Add(this.bcc);
            mailMessage.Subject = this.subject;
            mailMessage.Body = this.body;

            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.High;
            mailMessage.BodyEncoding = Encoding.UTF8;
            mailMessage.Headers.Add("X-Company", "Company");
            mailMessage.Headers.Add("X-Location", "Vietnamese");

            try
            {
                client.Send(mailMessage);
                mailMessage.Dispose();
                return true;
            }
            catch (Exception e)
            {
                if (logger.IsErrorEnabled)
                    logger.Error(e.Message);
                mailMessage.Dispose();
                return false;
            }
        }

        #endregion Methods

        #region Properties
        public string ServerName
        {
            get
            {
                return this.serverName;
            }
            set
            {
                this.serverName = value;
            }
        }
        public int EmailServerPort
        {
            get
            {
                return this.port;
            }
            set
            {
                this.port = value;
            }
        }
        public string Username
        {
            get
            {
                return this.username;
            }
            set
            {
                this.username = value;
            }
        }
        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
            }
        }
        public string From
        {
            get
            {
                return this.from;
            }
            set
            {
                this.from = value;
            }
        }
        public string To
        {
            get
            {
                return this.to;
            }
            set
            {
                this.to = value;
            }
        }
        public string Subject
        {
            get
            {
                return this.subject;
            }
            set
            {
                this.subject = value;
            }
        }
        public string Body
        {
            get
            {
                return this.body;
            }
            set
            {
                this.body = value;
            }
        }
        public bool SSL
        {
            get
            {
                return this.ssl;
            }
            set
            {
                this.ssl = value;
            }
        }
        public bool IsHtml
        {
            get
            {
                return this.isHtml;
            }
            set
            {
                this.isHtml = value;
            }
        }
        public string Bcc
        {
            get
            {
                return this.bcc;
            }
            set
            {
                this.bcc = value;
            }
        }
        public string Cc
        {
            get
            {
                return this.cc;
            }
            set
            {
                this.cc = value;
            }
        }
        #endregion Properties
    }
}